#include "sample_gas.h"

Sample_Gas::Sample_Gas(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    timer = new QTimer(this);
    timer->start(1000);
    ui_Combox_Init();
    ui_Btn_Init();
   ui_Label_Init();
   connect_Init();
   data_Init();
   Sysctrl->SYS_CTL_MODE = 0;

}


  void Sample_Gas::connect_Init()
  {

      //定时器
      connect(timer,&QTimer::timeout,[=](){
        data_Init();
      });

      //第一个选择下拉菜单
      connect(theComboBox,&MyComboBox::clicked,[=](){
          {
              theComboBox->showPopup();
              theComboBox->view()->setFocus();
              flag=1;
          }

      });

      connect(theComboBox,&QComboBox::currentTextChanged,[=](){
          {
              theLabel[1]->setText(theComboBox->currentText());
          }
      });
      connect(theComboBox2,&QComboBox::currentTextChanged,[=](){
          {

              theLabel[3]->setText(theComboBox2->currentText());
          }
      });

      //第二个选择下拉菜单
      connect(theComboBox2,&MyComboBox::clicked,[=](){
          {
              theComboBox2->showPopup();
              theComboBox2->view()->setFocus();
              flag=1;
          }

      });
      //确定
      connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
        data_Init();
        theLabel[4]->show();
        theLabel[5]->show();
        theLabel[6]->show();
        theLabel[7]->show();
        theLabel[8]->show();
        theLabel[9]->show();
      });

      //返回
      connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
          this->parentWidget()->show();
          this->close();
      });
  }

  void Sample_Gas::data_Init()
  {
      if(theComboBox->currentIndex() ==theComboBox->count()-1)
      {
          theLabel[5]->setText(QString::number(Sysstatus->Light_Data[0],'f',0));
          theLabel[8]->setText(QString::number(Sysstatus->PHOT_Noise_Ozo,'f',2));
      }
      else {
          theLabel[5]->setText(QString::number(Sysstatus->Light_Data[0],'f',0));
          theLabel[8]->setText(QString::number(Sysstatus->PHOT_Noise_Gas,'f',2));
      }

  }
  bool Sample_Gas::eventFilter(QObject *watched, QEvent *event)
  {

              if(btnlist_ui.at(0) == watched||btnlist_ui.at(1) == watched||theComboBox == watched||theComboBox->view() == watched||theComboBox2 == watched||theComboBox2->view() == watched)
          {
              if(event->type() == QEvent::KeyPress)
              {
                  //qDebug()<<focusWidget()->objectName();
                  QKeyEvent *ev = (QKeyEvent *)event;
                  QPushButton *button = (QPushButton *)watched;
                  MyComboBox *boBox = (MyComboBox *)watched;

                  if(flag==1)//当下拉显示的时候
                 {
                     switch (ev->key()) {
                     case UP:
                     {
                         //qDebug()<<"I";
                         if(focusWidget()== theComboBox) boBox = theComboBox;
                         if(focusWidget()== theComboBox2) boBox = theComboBox2;
                         if(boBox->currentIndex()>0)
                             boBox->setCurrentIndex(boBox->currentIndex() - 1);
                         else
                             boBox->setCurrentIndex(boBox->count()-1);
                             boBox->showPopup();
                         break;
                     }
                     case DOWN:
                     {
                         //qDebug()<<"K";
                         if(focusWidget()== theComboBox)
                         {
                             if(theComboBox->currentIndex() < theComboBox->count()-1)
                                 theComboBox->setCurrentIndex(theComboBox->currentIndex()+1);
                             else
                                 theComboBox->setCurrentIndex(0);
                             theComboBox->showPopup();
                         }
                         if(focusWidget()== theComboBox2)
                         {
                             if(theComboBox2->currentIndex() < theComboBox2->count()-1)
                                 theComboBox2->setCurrentIndex(theComboBox2->currentIndex()+1);
                             else
                                 theComboBox2->setCurrentIndex(0);
                             theComboBox2->showPopup();
                         }

                         break;
                     }
                     case OK:
                     {
                         if((flag==1))
                         {
                             theComboBox->hidePopup();
                             theComboBox2->hidePopup();
                             flag=2;
                         }
                         else
                         {
                             emit  button-> clicked();
                             emit boBox-> clicked();
                         }
                          break;
                     }
                     default:
                         break;
                     }
                 }
                 else
                 {
                     switch (ev->key())
                     {
                     case OK:
                     {

                         emit  button-> clicked();
                         emit  boBox-> clicked();
                       break;
                     }
                     case UP: //Up
                       {
//                         if(focusWidget() != btnlist_ui.first())
                              focusPreviousChild();
                         break;
                       }
                     case DOWN: //Down
                       {
//                         if(focusWidget() != btnlist_ui.back())
                              focusNextChild();
                         break;
                       }
                     case BACK:
                     {
                         this->parentWidget()->show();
                         this->deleteLater();
                         break;
                     }
                     case INIT:
                     {
                         init_Show *show = new init_Show(this);
                         show->show();
                         this->hide();
                         break;
                     }
                      default:
                         break;
                     }
                 }
              }
//              else if (event->type()==QEvent::FocusIn)     //然后再判断控件的具体事件 (这里指获得焦点事件)
//                        {
//                            QPushButton *focus_button = (QPushButton *)watched;
//                                    focus_button->setFixedSize(160,60);
//                        }
//              else if (event->type()==QEvent::FocusOut)    // 这里指 控件的失去焦点事件
//                       {
//                  QPushButton *focus_button = (QPushButton *)watched;
//                          focus_button->setFixedSize(120,50);
//                       }
              }
              return QMainWindow::eventFilter(watched,event);//返回父类的事件过滤器
  }

  void Sample_Gas::ui_Label_Init()
  {

      for(int i=0;i<13;i++)//创建界面显示的label
      {
           theLabel[i] = new QLabel(this);
           theLabel[i]->setStyleSheet("QLabel{\
                                  outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                  background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                                  border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                  border-style: outset;\
                                  /* 按钮文本设置 */\
                                  qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                  font-size: 20px;\
                                  color: #123567;			                 /* 文本颜色 */\
                             }"
            );
      }
      theLabel[0]->setGeometry(50,160,200,30);//设定每隔放置一个按键
      theLabel[1]->setGeometry(300,160,100,30);//设定每隔放置一个按键
      theLabel[2]->setGeometry(50,200,200,30);//设定每隔放置一个按键
      theLabel[3]->setGeometry(300,200,100,30);//设定每隔放置一个按键
      theLabel[4]->setGeometry(50,240,200,30);//设定每隔放置一个按键
      theLabel[5]->setGeometry(300,240,100,30);//设定每隔放置一个按键
      theLabel[6]->setGeometry(420,240,100,30);//设定每隔放置一个按键
      theLabel[7]->setGeometry(50,280,200,30);//设定每隔放置一个按键
      theLabel[8]->setGeometry(300,280,100,30);//设定每隔放置一个按键
      theLabel[9]->setGeometry(420,280,100,30);//设定每隔放置一个按键
      theLabel[10]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
      theLabel[10]->setStyleSheet("QLabel{"
                            + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
      theLabel[11]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
      theLabel[12]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

     theLabel[10]->setGeometry(0,0,640,60);//设定每隔放置一个按键
     theLabel[11]->setGeometry(0,0,300,60);//设定每隔放置一个按键
     theLabel[12]->setGeometry(340,0,300,60);//设定每隔放置一个按键
     theLabel[10]->setText("光强检测");

      theLabel[0]->setText(tr("当前选择为："));
      theLabel[1]->setText(theComboBox->currentText());
      theLabel[2]->setText(tr("当前选择的跨度为："));
      theLabel[3]->setText(theComboBox2->currentText());
      theLabel[4]->setText(tr("光强："));
      theLabel[5]->setText(QString::number(Sysset->Gas[temp_Gas].gasconcentration_span[temp_Gas_select],'f',3));
      theLabel[6]->setText(tr("Hz"));
      theLabel[7]->setText(tr("噪声："));
      theLabel[8]->setText(QString("%1").arg(Sysset->Gas[temp_Gas].totalflow_span[temp_Gas_select]));
      theLabel[9]->setText(tr("Hz"));
      theLabel[2]->hide();
      theLabel[3]->hide();
      theLabel[4]->hide();
      theLabel[5]->hide();
      theLabel[6]->hide();
      theLabel[7]->hide();
      theLabel[8]->hide();
      theLabel[9]->hide();
  }

  void Sample_Gas::ui_Combox_Init()
  {


          QStringList strList;
          QStringList strList2;

      strList<<"参比气体"<<"样气";
      strList2<<"零气"<<"标气1"<<"标气2"<<"标气3"<<"标气4"<<"标气5"<<"标气6"<<"自动";

      theComboBox = new MyComboBox(this);
      theComboBox2 = new MyComboBox(this);
theComboBox2->hide();
      theComboBox->addItems(strList);
      theComboBox->setGeometry(120,70,160,60 );
      theComboBox->installEventFilter(this);
      theComboBox->view()->installEventFilter(this);
      theComboBox2->addItems(strList2);
      theComboBox2->setGeometry(360,70,160,60 );
      theComboBox2->installEventFilter(this);
      theComboBox2->view()->installEventFilter(this);

      theComboBox->setStyleSheet("QComboBox{color:white;      /* 字体颜色*/\
                                 background-color: rgb(39, 170, 255);\
                                 border-radius:4px;\
                                 border: 1px groove white; \
                                 padding:1px 30px;\
                                 background-position: content;                                            /* 文本的位置 */\
                                 font-size: 25px;\
                                 color: #000000;			                 /* 文本颜色 */\
                                 border-style: outset;}"
      "QComboBox:focus{color:rgb(250,250, 250);\
                                  background-color: rgb(255, 170, 39);\
                                  border-radius:4px;\
                                  border: 2px rgb(110, 111, 215); \
                                  background-position: content;                                            /* 文本的位置 */\
                                  padding:1px 30px;\
                                  font-size: 25px;\
                                  color: #f11111;			                 /* 文本颜色 */\
                                    border-style: outset;}"
      );
       theComboBox2->setStyleSheet(theComboBox->styleSheet());




  }

  void Sample_Gas::ui_Btn_Init()
  {


      for(int i=0;i<2;i++)//创建主屏幕显示的按键
      {

           theBtn[i] = new QPushButton(this);
  //        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
          theBtn[i]->setFixedSize(160,60);
          theBtn[i]->geometry();
}
  btnlist_ui.append(theBtn[0]);
  btnlist_ui.append(theBtn[1]);

  theBtn[0]->setStyleSheet(  "QPushButton{"+theBox_style[0]+ "background-image: url(:/pic/切换.png); /* 背景图片 */"+"}"
   "QPushButton:focus{"+theBox_style[1]+ "background-image: url(:/pic/切换2.png); /* 背景图片 */"+"}");
  theBtn[1]->setStyleSheet(  "QPushButton{"+theBox_style[0]+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
   "QPushButton:focus{"+theBox_style[1]+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");

  btnlist_ui.at(0)->setGeometry(120,360,160,60 );
  btnlist_ui.at(1)->setGeometry(360,360,160,60 );
  btnlist_ui.at(0)->setText(tr("    确定"));
  btnlist_ui.at(1)->setText(tr("    返回"));

  //事件过滤器
  foreach (QPushButton *btn, btnlist_ui) {
      btn->installEventFilter(this);
  }
  }
